package application
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class Application
	{
		private static var instance:Application;
		
		public static function get():Application {
			if (instance == null) {
				instance = new Application();
			}
			return instance;
		}
		
		public function Application()
		{
		}
		
		private var attributes:Array = new Array();
		
		private var attributeListeners:ArrayCollection = new ArrayCollection();

		public function addAttribute(attrName:String, attrValue:Object):void {
			
			attributes.push({name:attrName, value:attrValue});
			
			for(var i:int = 0; i < attributeListeners.length; i++) {
				var listener:AttributeListener = attributeListeners.getItemAt(i) as AttributeListener;
				listener.onAttributeAdded(attrName, attrValue);
			}
		}
		
		public function getAttribute(attrName:String):Object {
			
			for (var i:int = 0; i < attributes.length; i++) {
				var name:String = attributes[i]['name'];
				if (name == attrName) {
					return attributes[i]['value'];
				}
			}

			return null;

//			var indexOf:int = attributes.indexOf(attrName);
//			
//			Alert.show("Procurando AttrName: " + attrName);
//			
//			Alert.show("IndexOF: " + indexOf);
//			
//			if (indexOf > -1) {
//				return attributes[indexOf];
//			} else {
//				return null;
//			}
		}

		public function removeAttribute(attrName:String):void {
			var indexOf:int = attributes.lastIndexOf(attrName);
			
			if (indexOf > -1) {
				var attrValue:Object = attributes[indexOf];
				
				delete attributes[indexOf];
				
				for(var i:int = 0; i < attributeListeners.length; i++) {
					var listener:AttributeListener = attributeListeners.getItemAt(i) as AttributeListener;
					listener.onAttributeRemoved(attrName, attrValue);
				}
			}
		}

	}
}